package com.scm.biz.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.bean.Constant;
import com.scm.biz.bean.SampleStore;
import com.scm.biz.dto.SampleStoreDto;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.query.SampleStoreQuery;
import com.scm.biz.result.SampleStoreResult;
import com.scm.biz.service.ConstantService;
import com.scm.biz.service.SampleStoreService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.core.bean.resp.RespBean;
import com.scm.utils.Const;

/**
 * 样衣的控制器类
 * @author Young
 * @Date 2016.11.23
 */
@Controller
@RequestMapping("/designstore")
public class SampleStoreAction {
	
	@Resource
	private SampleStoreService sampleStoreService;
	
	@Resource
	private ConstantService constantService;
	
	/**
	 * 添加样衣
	 * @param sampleStore
	 * @param req
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/add")
	@ResponseBody
	public RespBean<Integer> addSampleStore(SampleStore sampleStore,MultipartHttpServletRequest req) throws Exception{
		RespBean<Integer> respBean=new RespBean<Integer>();
		if(sampleStore.getSeason()==null||sampleStore.getSeason().trim().isEmpty()){
			throw new UfdmException("季节款式为空");
		}
		if(sampleStore.getSex()==null||sampleStore.getSex().trim().isEmpty()){
			throw new UfdmException("性别为空");
		}
		if(sampleStore.getCategory()==null||sampleStore.getCategory().trim().isEmpty()){
			throw new UfdmException("样衣分类为空");
		}
		sampleStoreService.save(sampleStore,req);
		respBean.setBody(sampleStore.getId());
		return respBean;
	}
	
	/**
	 * 根据查询参数，获取样衣列表
	 * @param query
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView getList(SampleStoreQuery query,HttpServletRequest req){
		ModelAndView mav=new ModelAndView("sampleStore/sampleStore_list");
		if(query.getPageNo()==null||query.getPageNo()==0){
			query.setPageNo(1);
		}
		List<SampleStoreDto> list=sampleStoreService.getList(query);
		mav.addObject("sampleStoreList", list);
		mav.addObject("splitPage", query.getSplitPage());
		mav.addObject("query",query);
		mav.addObject("path", Const.getPath(req));
		return mav;
	}
	
	/**
	 * 根据ID，获得样衣
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	@ResponseBody
	public RespBean<SampleStoreResult> doShowDetailPage(int id){
		RespBean<SampleStoreResult> respBean=new RespBean<SampleStoreResult>();
		SampleStoreResult sampleStore=sampleStoreService.getById(id);
		respBean.setBody(sampleStore);
		return respBean;
	}
	
	/**
	 * 修改样衣
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public RespBean<String> doEdit(SampleStore sampleStore,MultipartHttpServletRequest req) throws Exception{
		RespBean<String> respBean=new RespBean<String>();
		if(sampleStore.getSeason()==null||sampleStore.getSeason().trim().isEmpty()){
			throw new UfdmException("季节款式为空");
		}
		if(sampleStore.getSex()==null||sampleStore.getSex().trim().isEmpty()){
			throw new UfdmException("性别为空");
		}
		if(sampleStore.getCategory()==null||sampleStore.getCategory().trim().isEmpty()){
			throw new UfdmException("样衣分类为空");
		}
		sampleStoreService.modifyById(sampleStore,req);
		respBean.setBody("success");
		return respBean;
	}
	
	/**
	 * 显示添加样衣页面
	 * @return
	 */
	@RequestMapping("/doShowAddPage")
	public ModelAndView doShowAddPage(){
		return new ModelAndView("sampleStore/sampleStore_add");
	}
	
	/**
	 * 获得样衣的所有分类
	 * @return
	 */
	@RequestMapping("/getCategory")
	@ResponseBody
	public RespBean<List<Constant>> getCategory(){
		RespBean<List<Constant>> respBean=new RespBean<List<Constant>>();
		List<Constant> list=constantService.getConstantByCategory(FileCategoryEnum.SAMPLE_CATEGORY.value());
		respBean.setBody(list);
		return respBean;
	}
	
}
